<?php

class TorneosAdminController extends \BaseController {

	public function __construct(){
		
		$roles=array(
				'redactor'=>4,
				'admin'=>1,
				'usuario'=>'2',
				'admin.torneos'=>3
		);
		
		//permisos
		$rolesTorneos=array($roles['admin.torneos'],$roles['admin']);
		
		$this->beforeFilter('roles:'.implode($rolesTorneos,'-'));
	}
	
	
	/**
	 * Display a listing of noticiasadmins
	 *
	 * @return Response
	 */
	public function index()
	{
		$torneos = Torneo::all();
		
		return View::make('contents.admin.torneos.index', compact('torneos'));
	}

	/**
	 * Show the form for creating a new noticiasadmin
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('contents.admin.torneos.create');
	}

	/**
	 * Store a newly created noticiasadmin in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Torneo::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$torneo = new Torneo();
		$torneo->fill($data);
		$torneo->user()->associate(Auth::user());
		$torneo->campo()->associate(Campo::find(Input::get('campo')));
		$torneo->save();

		return Redirect::route('admin.torneos.index');
	}

	/**
	 * Display the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$torneo = Torneo::findOrFail($id);

		return View::make('contents.admin.torneos.show', compact('torneo'));
	}

	/**
	 * Show the form for editing the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$torneo = Torneo::find($id);

		return View::make('contents.admin.torneos.edit', compact('torneo'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$torneo = Torneo::findOrFail($id);

		$validator = Validator::make($data = Input::all(), Torneo::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		$torneo->update($data);
		
		$torneo->campo()->associate(Campo::find(Input::get('campo')));
		$torneo->save();
		
		return Redirect::route('admin.torneos.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		Torneo::destroy($id);

		return Redirect::route('admin.torneos.index');
	}

}